package module.main.view.mainInterface.friendListPanel
{
	import com.darwin.darwinUI.core.UIComponent;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	/**
	 * 图片，外部加载
	 * @author 
	 */	
	public class Image extends UIComponent
	{
		private var _loader:Loader;
		private var _source:Object;
		private var _defaultImage:DisplayObject;
		private var $width:Number = 50;
		private var $height:Number = 50;
		
		public function Image()
		{
			super();
		}
		
		override public function get width():Number
		{
			return _width;
		}
		
		override public function set width(value:Number):void
		{
			_width = value;
			setSize();
		}
		
		override public function get height():Number
		{
			return $height;
		}
			
		override public function set height(value:Number):void
		{
			$height = value;
			setSize();
		}
		
		public function get source():Object
		{
			return _source;
		}
		
		private var display:DisplayObject;
		
		public function set source(value:Object):void
		{
			if(_source == value) return;
			_source = value;
			if(_source == null)
			{
				hideLoader();
				return;
			}
			if(display != null && contains(display))
			{
				removeChild(display);
			}
			if(_source is DisplayObject)
			{
				display = _source as DisplayObject;
				addChild(display);
				return;
			}
			if (_source is String && _source != "")
			{
				var $picUrl:String = _source as String;
				showLoader();
				if(_loader == null)
				{
					_loader = new Loader();
					_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadCompletedHandler);
					_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
					addChild(_loader);
				}
				_loader.load(new URLRequest($picUrl));
			}else{
				hideLoader();
			}
		}
		
		private function onLoadCompletedHandler(e:Event):void
		{
			if(_defaultImage)
			{
				if(_loader.width == 0 || _loader.height == 0)
				{
					_defaultImage.visible = true;
				}else{
					_defaultImage.visible = false;
				}
			}
			setSize();
		}
		
		private function setSize():void
		{
			if(_loader == null) return;
			_loader.scrollRect = null;
			_loader.width = this.width;
			_loader.height = this.height;
		}
		
		private function onIoError(e:IOErrorEvent):void
		{
			
		}
		
		public function get defaultImage():DisplayObject
		{
			return _defaultImage;
		}
		
		public function set defaultImage(value:DisplayObject):void
		{
			if(_defaultImage != null && this.contains(_defaultImage))
			{
				removeChild(_defaultImage);
			}
			if(value != null)
			{
				addChildAt(_defaultImage, 0);
			}
			_defaultImage = value;
		}
		
		private function hideLoader():void
		{
			if(_defaultImage)
			{
				_defaultImage.visible = true;
			}
			if(_loader)
			{
				_loader.visible = false;
			}
		}
		
		private function showLoader():void
		{
			if(_defaultImage)
			{
				_defaultImage.visible = false;
			}
			if(_loader)
			{
				_loader.visible = true;
			}
		}
	}
}